package com.meiyue.library.custom.controller;

import com.meiyue.bean.base.PageModel;
import com.meiyue.bean.protocol.NetRequest;
import com.meiyue.bean.protocol.NetResult;
import com.meiyue.library.custom.service.BaseService;
import com.meiyue.library.model.net.IdRequest;
import com.meiyue.library.model.net.PageRequest;
import com.meiyue.library.model.net.StringRequest;
import com.meiyue.library.utils.ResultUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.Serializable;
import java.util.List;

/**
 * MeiyueJavaProject
 * com.meiyue.library.custom.controller
 * Created by Chris Chen
 * 2017/9/15
 * Explain:通用控制器接口 Body请求方式
 */
public abstract class BaseBodyController<T, I extends Serializable, S extends BaseService> {
    public abstract S getService();

    //获取一个数据
    @RequestMapping("/getOne")
    public NetResult<T> getOne(@RequestBody NetRequest<IdRequest<I>> body) {
        return ResultUtils.buildResult((T) (getService().getOne(body.getParams().getId())));
    }

    //获取数据列表
    @RequestMapping("/getList")
    public NetResult<List<T>> getList(@RequestBody NetRequest body) {
        return ResultUtils.buildResult(getService().getList());
    }

    //获取分页
    @RequestMapping("/getPage")
    public NetResult<PageModel<T>> getPage(@RequestBody NetRequest<PageRequest> body) {
        return ResultUtils.buildResult(getService().getPage(body.getParams().getPage(), body.getParams().getPageSize()));
    }

    //删除一条
    @RequestMapping("/removeOne")
    public NetResult<Boolean> removeOne(@RequestBody NetRequest<IdRequest<I>> body) {
        return ResultUtils.buildResult(getService().removeOne(body.getParams().getId()));
    }

    //删除一批
    @RequestMapping("/removeList")
    public NetResult<Boolean> removeList(@RequestBody NetRequest<StringRequest> body) {
        return ResultUtils.buildResult(getService().removeList(body.getParams().getStr()));
    }
}
